package com.zxl.always.on.display.ui.widget.battery;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.BatteryManager;
import android.util.AttributeSet;
import android.view.View;

import com.zxl.always.on.display.R;
import com.zxl.always.on.display.tools.GraphicUtils;
import com.zxl.always.on.display.tools.LogUtils;

/**
 * Created by Administrator on 2018/2/27.
 */

public class BatteryMeterView extends View {

    private Drawable mBgDrawable, mChargDrawable;

    private int mWidth, mHeight;

    private Paint mPaint;

    private float mBatteryLevel;
    private int mBatteryStatus;

    public BatteryMeterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mBgDrawable = getResources().getDrawable(R.drawable.stat_sys_battery_aod);
        mChargDrawable = getResources().getDrawable(R.drawable.stat_sys_battery_charging_aod);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.WHITE);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mBgDrawable.setColorFilter(new PorterDuffColorFilter(-3355444, PorterDuff.Mode.SRC_ATOP));
        mBgDrawable.setBounds(0, 0, this.mWidth, this.mHeight);
        mBgDrawable.draw(canvas);

        int width = mBgDrawable.getIntrinsicWidth();
        int height = mBgDrawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas localCanvas = new Canvas(bitmap);
        mBgDrawable.draw(localCanvas);

        int top = height;
        for (int i = 0; i < height; i++) {
            int value = bitmap.getPixel(width / 2, i);
            int a = Color.alpha(value);
            if (a == 0) {
                top = Math.min(i, top);
            }
        }
        Rect rect = new Rect(0, top, width, height);
        Rect levelRect = new Rect(0, 0, rect.width(), (int) (mBatteryLevel * rect.height()));
        levelRect.offset(0, rect.bottom - levelRect.height());
        mPaint.setColorFilter(new PorterDuffColorFilter(-3355444, PorterDuff.Mode.SRC_ATOP));
        canvas.drawRect(levelRect, mPaint);

        if (mBatteryStatus == BatteryManager.BATTERY_STATUS_CHARGING) {
            int[] colors = {-15461356, -15461356, -3355444, -3355444};
            float chargeLevel = mBatteryLevel;
            float[] positions = {0.0F, chargeLevel, chargeLevel, 1.0F};
            BitmapDrawable chargeDrawable = GraphicUtils.getLinearGradientDrawable(getContext(), mChargDrawable, this.mWidth, this.mHeight, 0.0F,
                    new LinearGradient(0.0F, rect.bottom, 0.0F, rect.top, colors, positions, Shader.TileMode.CLAMP));
            if (chargeDrawable != null) {
                chargeDrawable.setBounds(0, 0, mWidth, mHeight);
                chargeDrawable.draw(canvas);
            }
        }
    }

    public void setBattery(Intent intent) {
        mBatteryStatus = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        mBatteryLevel = level / (float)scale;
        LogUtils.d("battery: " + level + ", " + scale + ", " + mBatteryLevel);
    }

    public float getmBatteryLevel() {
        return mBatteryLevel;
    }
}
